package com.xypower.web.servlet;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xypower.pojo.Classes;
import com.xypower.pojo.R;
import com.xypower.pojo.Student;
import com.xypower.service.ClassesService;
import com.xypower.service.StudentService;
import com.xypower.service.impl.ClassesServiceImpl;
import com.xypower.service.impl.StudentServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {
    private StudentService studentService;
    @Override
    public void init() throws ServletException {
        studentService = new StudentServiceImpl();
    }
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //0. 处理post请求的中文乱码
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf8");
        //1. 得到请求参数cmd
        String cmd = req.getParameter("cmd");
        //2. 根据参数调用不同的方法
        if(StrUtil.isNotBlank(cmd)){
            if("list".equals(cmd)){         //列表所有学生
                list(req,resp);
            }else if("add".equals(cmd)){    //添加学生
                add(req,resp);
            }else if("update".equals(cmd)){ //修改学生
                update(req,resp);
            }else if("delete".equals(cmd)){ //删除学生
                delete(req,resp);
            }
        }
    }



    //1. 列表所有学生
    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.1 查询所有学生
        List<Student> students = studentService.findAll();
        //1.2将上面的集合转换为json字符串
        String string = JSON.toJSONString(students);
//        1.3输出到页面
        resp.getWriter().println(string);
        resp.getWriter().flush();
    }

    //2. 添加学生
    private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        得到学生的信息
        String stud = req.getParameter("stud");
        Student student = JSON.parseObject(stud,Student.class);
        studentService.insert(student);
        resp.getWriter().println(JSON.toJSONString(R.ok()));
        resp.getWriter().flush();
    }

    //3. 修改学生
    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String stud = req.getParameter("stud");
        Student student = JSON.parseObject(stud,Student.class);
        studentService.update(student);
        resp.getWriter().println(JSON.toJSONString(R.ok()));
        resp.getWriter().flush();
    }

    //4. 删除学生
    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //4.1 得到要删除的学生id
        String sid = req.getParameter("sid");
        //4.2 开始删除
        studentService.delete(sid);
        //4.3 返回R对象
        resp.getWriter().println(JSON.toJSONString(R.ok()));
        resp.getWriter().flush();

    }
}
